package com.eroad.product.tools;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;


/**
 * 气泡图
 */
public class BubbleView extends View{

	private Context context;
	private ArrayList<String> xTag;		//x轴数值
	private double[] yStart;			//每天上班时间
	private double[] yEnd;				//每天的下班时间
	private float density;				//屏幕密度
	private int colorXY ;				//xy轴颜色
	private int colorStart;				//上班折线颜色
	private int colorEnd;				//下班折线颜色
	private int currentWeek;			// 1-7 
	private float LINE_HEIGHT;	//Y轴高度
	private float LINE_X;		//x轴数据间隔
	private float LINE_WIDTH;	//X轴宽度
	private Paint mPaint;		//灰线画笔
	private float max  = 24;	//一天24小时
	private int time_begin;		//规定的上班时间点
	private int time_end;		//规定的下班时间点
	private Paint paintStart;	//上班时间画笔
	private Paint paintEnd;		//下班时间画笔
	private Paint paintFill;	//白色画笔
	private float xPoint;		//x轴留白
	private float yPoint;		//y轴留白


	public BubbleView(Context context) {
		super(context);
	}

	public BubbleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context = context;
		//获取屏幕密度
		Display display = ((Activity)context).getWindowManager().getDefaultDisplay();
		DisplayMetrics displayMetrics = new DisplayMetrics();
		display.getMetrics(displayMetrics);
		density = displayMetrics.density;
	}

	public BubbleView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		this.init(); 		//初始化颜色数值
		this.initPaint();	//初始化画笔
		for(int i = 0;i<xTag.size();i++){
			canvas.drawLine(xPoint+i*LINE_X, 0, xPoint+i*LINE_X, LINE_HEIGHT, mPaint);	//y轴平行线
			canvas.drawText(getDateOfWeek(i), xPoint+i*LINE_X-mPaint.measureText(getDateOfWeek(i))/2, LINE_HEIGHT + (10*density), mPaint);	//X周数值10-10
			canvas.drawText(xTag.get(i), xPoint+i*LINE_X-mPaint.measureText(xTag.get(i))/2, LINE_HEIGHT + (20*density), mPaint);			//X周数值（周几）
		}
		for (int i = 0 ; i <7; i++){
			canvas.drawLine(xPoint, i*LINE_HEIGHT/6, LINE_WIDTH, i*LINE_HEIGHT/6, mPaint);//x轴平行线
		}
		setTimeLine(canvas, (float) 12.00, Color.parseColor("#1f65be"));//虚线+Y轴数值
		setTimeLine(canvas, (float) time_begin,colorStart);				//虚线+Y轴数值
		setTimeLine(canvas, (float) time_end,colorEnd);					//虚线+Y轴数值
		int radius = 5;
		for(int i = 0; i < 6; i++){
			Path pathStart = new Path(); //定义一条路径 
			Path pathEnd = new Path();  //定义一条路径 
			if(i<yStart.length-1){
				pathStart.moveTo(xPoint+i*LINE_X, (float) ( LINE_HEIGHT * (yStart[i])/max));
				pathStart.lineTo(xPoint+(i+1)*LINE_X, (float) ( LINE_HEIGHT * (yStart[i+1])/max)); 
				canvas.drawPath(pathStart, paintStart);  //上班折线
			}

			if(i<yEnd.length-1){
				pathEnd.moveTo(xPoint+i*LINE_X, (float) ( LINE_HEIGHT * (yEnd[i])/max));
				pathEnd.lineTo(xPoint+(i+1)*LINE_X, (float) ( LINE_HEIGHT * (yEnd[i+1])/max)); 
				canvas.drawPath(pathEnd, paintEnd);     //下班折线
			}

			if(currentWeek-2 ==  i){
				radius  = 10;    //当天大圆
			}else{
				radius  = 5;    //不是当天的小圆
			}

			if(i<yStart.length){
				canvas.drawCircle(xPoint+i*LINE_X, (float) ( LINE_HEIGHT * (yStart[i])/max), radius, paintStart);
				canvas.drawCircle(xPoint+i*LINE_X, (float) ( LINE_HEIGHT * (yStart[i])/max), radius-1, paintFill);//画白圆覆盖
			}
			if(i<yEnd.length){
				canvas.drawCircle(xPoint+i*LINE_X, (float) ( LINE_HEIGHT * (yEnd[i])/max), radius, paintEnd);
				canvas.drawCircle(xPoint+i*LINE_X, (float) ( LINE_HEIGHT * (yEnd[i])/max), radius-1, paintFill);  //画白圆覆盖
			}
		}

	}

	public void setData(ArrayList<String> xTag,double[] yStart,double[] yEnd,int begin,int end){
		this.xTag = xTag;
		this.yStart = yStart;
		this.yEnd = yEnd;
		this.time_begin = begin;
		this.time_end = end;
		postInvalidate();  //重绘
	}

	/**
	 * 初始化颜色大小
	 */
	public void init(){
		colorXY=  Color.parseColor("#dedede");
		colorStart=  Color.parseColor("#16ABEB");
		colorEnd=  Color.parseColor("#E2731F");
		currentWeek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);

		xPoint = getYData(max).length()*8*density;
		yPoint = 40*density;
		LINE_X = (this.getWidth()-2*xPoint)/(xTag.size()-1);
		LINE_HEIGHT = this.getHeight() - yPoint;
		LINE_WIDTH = this.getWidth()-xPoint;
	}

	/**
	 * 初始化画笔
	 */
	public void initPaint(){
		mPaint = new Paint();
		mPaint.setStyle(Style.FILL);
		mPaint.setStrokeWidth(1);
		mPaint.setAntiAlias(true);
		mPaint.setTextSize(10*density); 
		mPaint.setColor(Color.GRAY); 

		paintStart = new Paint();// 上班时间画笔
		paintStart.setStyle(Style.STROKE); 
		paintStart.setStrokeWidth(3);
		paintStart.setColor(colorStart);
		paintStart.setAntiAlias(true);

		paintEnd = new Paint();// 下班时间画笔
		paintEnd.setStyle(Style.STROKE); 
		paintEnd.setStrokeWidth(3);
		paintEnd.setColor(colorEnd);
		paintEnd.setAntiAlias(true);

		paintFill = new Paint();//白色画笔
		paintFill.setStyle(Style.FILL); 
		paintFill.setStrokeWidth(1);
		paintFill.setColor(Color.WHITE);
		paintFill.setAntiAlias(true);
	}

	/**
	 * 画平行于X轴的虚线
	 * @param canvas
	 * @param time
	 * @param color
	 */
	public void setTimeLine(Canvas canvas,float time,int color){
		Paint paint = new Paint();      
		paint.setStyle(Paint.Style.STROKE);      
		paint.setColor(color);
		paint.setAntiAlias(true);

		Path path = new Path();           
		path.moveTo(xPoint,(float) ( LINE_HEIGHT * time/max));      
		path.lineTo(LINE_WIDTH,(float) ( LINE_HEIGHT * time/max)); 
		PathEffect effects = new DashPathEffect(new float[]{5,5,5,5},1);      
		paint.setPathEffect(effects);
		canvas.drawPath(path, paint); //画虚线

		paint.setStyle(Paint.Style.FILL); 
		canvas.drawRoundRect(new RectF(5*density, ( LINE_HEIGHT * time/max)-(8*density), xPoint-(5*density), (LINE_HEIGHT * time/max)+ (8*density)), 3*density,3*density,paint);//画圆角矩形背景

		Paint mPaint = new Paint();   
		mPaint.setStyle(Style.FILL);
		mPaint.setStrokeWidth(1);
		mPaint.setAntiAlias(true);
		mPaint.setTextSize(10*density); 
		mPaint.setColor(Color.WHITE);
		canvas.drawText(getYData(time), 7*density, ( LINE_HEIGHT * time/max)+(3*density), mPaint);//画Y轴数值
	}

	/**
	 * 获取本周周几对应的日期（10-10）
	 * @param index
	 * @return
	 */
	public String getDateOfWeek(int index){
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
		if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
			cal.add(Calendar.WEEK_OF_MONTH, -1);
		}
		cal.set(Calendar.DAY_OF_WEEK, 2);
		cal.add(Calendar.DATE, index);
		return sdf.format(cal.getTime());

	}

	/**
	 * 获取Y轴固定格式的数值
	 * @param time
	 * @return
	 */
	public String getYData(float time){
		Date date = new Date(0, 0, 0,(int)(time*60/60),(int)(time*60%60));
		return String.format("%tR", date);
	}

}
